﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PolePositionMobileUtilityData;
using System.Threading;
using System.Data.SqlServerCe;
using System.Collections;
using PolePositionMobileUtilityGeneral;
using PolePositionMobileUtilityEntity;

namespace PolePositionMobileUtilityLogic
{
    public class LUtilityInspectionResult
    {
        /// <summary>
        /// Return a DataRow with all InspectionResult data, if has error return null
        /// </summary>
        /// <param name="inspectionResultId"></param>
        /// <returns></returns>
        public static EUtilityInspectionResult GetById(int inspectionResultId)
        {
            DataRow dr = DInspectionResult.GetById(inspectionResultId);
            EUtilityInspectionResult _utilityResult = new EUtilityInspectionResult();
            //if(dr["Comments"] != null)
            //    _utilityResult.Comments = ConvertMobileHelper.Tostring(dr["Comments"]);
            _utilityResult.Id = ConvertMobileHelper.ToInt32(dr["UtilityInspectionResultId"]);
            _utilityResult.Inspection = LUtilityInspection.GetById(ConvertMobileHelper.ToInt32(dr["UtilityInspectionFk"]));
            _utilityResult.InspectionType = new EInspectionType(ConvertMobileHelper.ToInt32(dr["InspectionTypeFk"]), ConvertMobileHelper.Tostring(dr["InspectionType_Name"]));
            _utilityResult.LastInspection = ConvertMobileHelper.Tostring(dr["LastInspection"]);
            _utilityResult.ResultType = new EResultType(ConvertMobileHelper.ToInt32(dr["ResultTypeFk"]), ConvertMobileHelper.Tostring(dr["ResultType_Name"]));
            _utilityResult.Notes = LNote.GetByInspectionResult(_utilityResult.Id);
            _utilityResult.IsElectrical = bool.Parse(dr["isElectrical"].ToString());
            _utilityResult.Comments = dr["Comments"].ToString();
            return _utilityResult;
        }

        public static int Insert(EUtilityInspectionResult result)
        {
            SqlCeTransaction _Tr = null;
            SqlCeConnection _Con = null;
            try
            {
                _Con = Utils.GetConnection();
                _Tr = _Con.BeginTransaction();
                LUtilityInspection.Insert(result.Inspection);
                result.Id = DInspectionResult.Insert(result.Inspection.Id, result.InspectionType.Id, result.ResultType.Id, result.LastInspection, result.Comments,result.IsElectrical,_Tr);
                LNote.Insert(result.Notes, result.Id, _Tr);
                _Tr.Commit();
                return result.Id;
            }
            catch (Exception _Ex)
            {
                if (_Tr != null)
                    _Tr.Rollback();
                throw _Ex;
            }
            finally
            {
                if (_Con != null)
                {
                    _Con.Close();
                }
            }
        }

        public static void Update(EUtilityInspectionResult result)
        {
            SqlCeTransaction _Tr = null;
            SqlCeConnection _Con = null;
            try
            {
                _Con = Utils.GetConnection();
                _Tr = _Con.BeginTransaction();
                LNote.DeleteByInspectionResult(result.Id, _Tr);
                LNote.Insert(result.Notes, result.Id, _Tr);
                LUtilityInspection.Update(result.Inspection,_Tr);
                DInspectionResult.Update(result.Id, result.Inspection.Id, result.InspectionType.Id, result.ResultType.Id, result.LastInspection, result.Comments,result.IsElectrical,_Tr);
                _Tr.Commit();
            }
            catch (Exception _Ex)
            {
                if (_Tr != null)
                    _Tr.Rollback();
                throw _Ex;
            }
            finally
            {
                if (_Con != null)
                {
                    _Con.Close();
                }
            }
        }

        public static void Save(EUtilityInspectionResult result)
        {
            if (result.Id == Int32.MinValue)
            {
                Insert(result);
            }
            else
            {
                Update(result);
            }
        }

        public static void Delete(EUtilityInspectionResult result)
        {
            SqlCeTransaction _Tr = null;
            SqlCeConnection _Con = null;
            try
            {
                _Con = Utils.GetConnection();
                _Tr = _Con.BeginTransaction();
                LNote.DeleteByInspectionResult(result.Id, _Tr);
                DUtilityInspection.Delete(result.Inspection.Id,_Tr);
                DInspectionResult.Delete(result.Id,_Tr);
                _Tr.Commit();
            }
            catch (Exception _Ex)
            {
                if (_Tr != null)
                    _Tr.Rollback();
                throw _Ex;
            }
            finally
            {
                if (_Con != null)
                {
                    _Con.Close();
                }
            }
        }

        public static void ClearDatabase()
        {
            DInspectionResult.ClearDatabase();
        }
    }
}
